package club.zhilin.guli.service.edu.controller.backend;


import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.service.edu.domain.qo.CourseQueryObject;
import club.zhilin.guli.service.edu.domain.vo.CourseVO;
import club.zhilin.guli.service.edu.service.ICourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zhilin
 * @since 2022-01-11 15:29:01
 */
@RestController
@RequestMapping("/admin/edu/course")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CourseController {

    private final ICourseService iCourseService;

    @ApiOperation("新增课程")
    @PostMapping("/saveCourse")
    public CommonResult<String> saveCourse(@ApiParam(value = "课程基本信息", required = true) @RequestBody CourseVO courseVO) {
        String id = iCourseService.saveCourse(courseVO);
        return CommonResult.success("保存成功", id);
    }

    @ApiOperation("通过路径参数id查询课程")
    @GetMapping("/course/{id}")
    public CommonResult<CourseVO> getCourseById(@PathVariable String id) {
        return iCourseService.selectCourseById(id);
    }

    @ApiOperation("更新课程")
    @PutMapping("/course")
    public CommonResult<String> updateCourse(@RequestBody CourseVO courseVO) {
        return iCourseService.updateCourseVo(courseVO);
    }

    @ApiOperation("分页查询课程列表")
    @GetMapping("/list/{page}/{limit}")
    public CommonResult<Map<String, Object>> list(@ApiParam("页码") @PathVariable Long page, @ApiParam("页码显示数量") @PathVariable Long limit, @ApiParam("课程查询对象") CourseQueryObject courseQueryObject) {
        return iCourseService.selectList(page, limit, courseQueryObject);
    }

    @ApiOperation("通过课程id查询课程")
    @DeleteMapping("/remove/{id}")
    public CommonResult<String> removeCourseById(@PathVariable String id) {
        iCourseService.removeCoverById(id);
        boolean result = iCourseService.removeCoverByCourseId(id);
        return result ? CommonResult.success("删除成功") : CommonResult.failed("数据不存在");
    }


    @ApiOperation("通过日期查询发布课程数量")
    @GetMapping("/course/publish/count/{date}")
    public CommonResult<Integer> coursePublishCount(@PathVariable String date) {
        return CommonResult.success(iCourseService.selectCoursePublishCount(date));
    }
}

